package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.PmsBrandMapper;
import com.mail.model.db.PmsBrand;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsBrandApiImpl implements PmsBrandApi {

    @Autowired
    private PmsBrandMapper brandMapper;

    //获取全部品牌列表
    public List<PmsBrand> findAllBrand() {
        return brandMapper.selectList(null);
    }

    //根据品牌名称分页获取品牌列表
    public Page pageByKeyword(Integer pageNum, Integer pageSize, String keyword) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<PmsBrand> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(keyword), "name", keyword)
                .or()
                .like(StringUtils.isNotEmpty(keyword), "first_letter", keyword)
                .orderByDesc("sort");
        return brandMapper.selectPage(page, qw);
    }

    //根据编号查询品牌信息
    public PmsBrand findById(Long id) {
        return brandMapper.selectById(id);
    }

    //更新品牌
    public Integer updateBrand(PmsBrand brand) {
        return brandMapper.updateById(brand);
    }

    //批量删除品牌
    public Integer deleteByIds(List<Long> ids) {
        return brandMapper.deleteBatchIds(ids);
    }

    //添加品牌
    public Integer save(PmsBrand brand) {
        return brandMapper.insert(brand);
    }
}
